// pages/search/search.js
const app = getApp()
Page({
     data: {
          searchshoow:true,//显示历史搜索
          searchtext: '',//搜索内容
          pages: 1, //页数
          sorttype: 0, //排序状态
          num: 0, //导航动态标记
          yhj: '', //优惠券标记排序分式
          xl: '',//标记销量排序分式
          jg: '',//标记价格排序分式
          isshow:true,//显示历史搜索
          goodslist: [], //列表
     },
     onLoad: function (options) {
          var that = this
          that.setData({
               userid: wx.getStorageSync("userid")
          })
          // 判断标记排序分式
          if (this.data.yhj == "") {
               this.setData({
                    imageurlq: "/images/kong.png",
                    imageurl: "/images/kong.png",
                    imageurld: "/images/kong.png",
               })
          }
          that.read()
     },
     // 综合排序
     choosesorta: function (e) {
          var that = this
          that.setData({
               goodslist: [],
               num: e.currentTarget.dataset.num,
               imageurlq: "/images/kong.png",
               imageurl: "/images/kong.png",
               imageurld: "/images/kong.png",
               jg: '',
               yhj: '',
               yhj: '',
               sorttype: 0
          })
          that.request()
     },
     // 优惠券排序
     choosesortq: function (e) {
          var that = this
          that.setData({
               goodslist: [],
               num: e.currentTarget.dataset.num,
               imageurl: "/images/kong.png",
               imageurld: "/images/kong.png",
               jg: 0,
               xl: 0,
          })
          if (that.data.yhj == 0) {
               that.setData({
                    imageurlq: "/images/shang.png",
                    yhj: 1,
                    sorttype: 7
               })
          } else {
               that.setData({
                    imageurlq: "/images/xia.png",
                    yhj: 0,
                    sorttype: 8
               })
          }
          that.request()
     },
     // 销量排序
     choosesorts: function (e) {
          var that = this
          that.setData({
               goodslist: [],
               num: e.currentTarget.dataset.num,
               imageurld: "/images/kong.png",
               imageurlq: "/images/kong.png",
               jg: 0,
               yhj: 0,
          })
          if (that.data.xl == 0) {
               that.setData({
                    imageurl: "/images/shang.png",
                    xl: 1,
                    sorttype: 5
               })
          } else {
               that.setData({
                    imageurl: "/images/xia.png",
                    xl: 0,
                    sorttype: 6
               })
          }
          that.request()
     },
     // 价格排序
     choosesortd: function (e) {
          var that = this
          that.setData({
               goodslist: [],
               num: e.currentTarget.dataset.num,
               imageurl: "/images/kong.png",
               imageurlq: "/images/kong.png",
               xl: 0,
               yhj: 0,
          })
          if (that.data.jg == 0) {
               that.setData({
                    imageurld: "/images/shang.png",
                    jg: 1,
                    sorttype: 3
               })
          } else {
               that.setData({
                    imageurld: "/images/xia.png",
                    jg: 0,
                    sorttype: 4
               })
          }
          that.request()
     },
     // 获取搜词
     historytext: function (e) {
          var that=this
          that.setData({
               searchtext: e.target.dataset.text
          })
          that.request()
     },
     //   监听input
     inputing: function (e) {
          this.setData({
               searchtext: e.detail.value
          })
     },
     //   键盘搜索
     searchBtn: function () {
          var that = this
          that.Storage()
          that.read()
          that.request()
     },
     laji:function(){
          wx.setStorageSync('set', '')
          this.read()
     },
     icon_but: function () {
          this.setData({
               goodslist: [],
               searchtext: '',
               none:'',
               pages:1,
               isshow:true
          })
     },
     // 储存
     Storage: function () {
          var that = this
          var text = that.data.searchtext
          var tids = wx.getStorageSync('set');//读取
          var searchlist = "";
          var k = 0;
          try {
               var list = tids.split("#");
               for (var i = 0; i < list.length; i++) {
                    if (list[i].length != 0) {
                         var c_id = list[i];
                         if (c_id == text) {
                              k = 1
                         }
                         searchlist += c_id + "#";
                    }
               }
          } catch (e) {

          }

          if (k == 0) {
               searchlist += text + "#";
          }
          wx.setStorageSync('set', searchlist)//存
     },
     //  读取储存
     read: function () {
          var that = this
          var tids = wx.getStorageSync('set');//读取
          var lisi = tids.split("#");
          for (var i = 0; i < lisi.length; i++) {
               if (lisi[i] == "" || typeof (lisi[i]) == "undefined") {
                    lisi.splice(i, 1);
                    i = i - 1;
               }
          }
          that.setData({
               historylist: lisi
          })
     },
     request: function () {
          var that = this
          var pages = that.data.pages
          var searchtext = that.data.searchtext
          var sorttype = that.data.sorttype
          if (!(searchtext=="")){
               wx.showLoading({
                    title: '加载中...',
               })
               wx.request({
                    url: app.data.apiurl + '/ddk/search',
                    data: {
                         page: pages,
                         keyword: searchtext,
                         sortType: sorttype,
                         pageSize: 20
                    },
                    header: {
                         'content-type': 'application/json' // 默认值
                    },
                    method: 'GET',
                    dataType: 'json',
                    success: function (res) {
                         wx.hideLoading()
                         var rec = that.data.goodslist
                         var goodslist = res.data.data.goods_list
                         if (pages==1){
                              if(goodslist.length == 0) {
                                   that.setData({
                                        none: "没有找到你想要的商品"
                                   })
                              }
                         }else{
                              if (goodslist.length == 0) {
                                   that.setData({
                                        none: "没有商品了"
                                   })
                              }
                         }
                         
                         goodslist.forEach(function (item, index, array) {
                              if (((item.min_normal_price / 100).toString()).indexOf(".") == "-1") {
                                   array[index] = {
                                        img: item.goods_thumbnail_url,
                                        name: item.goods_name,
                                        id: item.goods_id,
                                        group: (item.min_group_price - item.coupon_discount) / 100,
                                        normal: item.min_group_price / 100,
                                        discount: item.coupon_discount / 100,
                                        quantity: item.sold_quantity
                                   }
                              } else {
                                   var nun = (item.min_normal_price / 100).toString().split(".")[1].length
                                   array[index] = {
                                        img: item.goods_thumbnail_url,
                                        name: item.goods_name,
                                        id: item.goods_id,
                                        group: ((item.min_group_price - item.coupon_discount) / 100).toFixed(nun),
                                        normal: item.min_group_price / 100,
                                        discount: item.coupon_discount / 100,
                                        quantity: item.sold_quantity
                                   }
                              }
                         });
                         for (var i = 0; i < goodslist.length; i++) {
                              rec.push(goodslist[i]);
                         }
                         that.setData({
                              goodslist: rec,
                              isshow: false
                         })
                    },
                    fail: function (res) {
                         wx.showToast({
                              title: '网络异常',
                              icon: 'none',
                              duration: 2000
                         })
                    }
               })

          }
     },
     onReachBottom: function () {
          var that = this
          var pages = that.data.pages
          pages++
          that.setData({
               pages: pages
          })
          that.request()

     },
     onShareAppMessage: function () {

     }
})